<?php

namespace App\Http\Controllers;
use App\Logic\MiGuLogic;
use Illuminate\Support\Facades\DB;

/**
 * RewardBox 领取累计登陆宝箱奖励
 */
class RewardBox extends Base
{

    public function logic()
    {

        $uid = $this->uid;
        if (!isset($this->param['box_id'])) {
            return $this->returnError(1, "param, box_id not set.\n");
        }
        $box_id = $this->param['box_id'];
        if ($box_id != 3 && $box_id != 7 && $box_id != 15 && $box_id != 21 && $box_id != 28) {
            return $this->returnError(2, "box_id error.\n");
        }

        $object = DB::select( "SELECT count(*) as count FROM t_reward_box_rec 
			where uid = $uid
			AND get_box_type = $box_id
			AND create_time >DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY) 
			AND create_time <DATE_ADD(CURDATE()-DAY(CURDATE())+1,INTERVAL 1 MONTH )");

        if ($object[0]->count > 0) {
            return $this->returnError(3, "你已经领取了该宝箱.\n");
        }

        if (! DB::table('t_reward_box_rec')->insert(["uid" => $uid, "get_box_type" => $box_id])) {
            return $this->returnError(4, "领取宝箱插入记录错误.\n");
        }

//        $add_gold     = 0;
//        $add_diamonds = 0;

        //暂时定义连续签到的奖品
        $prize = [
            3 => '10002:1|10032:1|10028:1',
            7 => '10003:1|10033:1|10036:1',
            15 => '10004:1|10034:1|10037:1',
            21 => '10005:1|10035:1|10038:1',
            28 => '10031:1|10027:1|10039:1',
        ];

//        if ($box_id == 3) {
//            $add_gold     = 10000;
//            $add_diamonds = 0;
//        } else if ($box_id == 7) {
//            $add_gold     = 30000;
//            $add_diamonds = 0;
//        } else if ($box_id == 15) {
//            $add_gold     = 0;
//            $add_diamonds = 50;
//        } else if ($box_id == 21) {
//            $add_gold     = 0;
//            $add_diamonds = 70;
//        } else if ($box_id == 28) {
//            $add_gold     = 0;
//            $add_diamonds = 100;
//        }
//        $ret = $this->grant_diamonds($uid, $add_diamonds, "签到宝箱", (string)$box_id,0,1,100004);
//        if (!$ret) {
//           return $this->returnError(5, "签到奖励钻石时，服务器发生数据错误.");
//        }
//        $ret = $this->grant_golds($uid, $add_gold, "签到宝箱", (string)$box_id);
//		if (!$ret) {
//            return $this->returnError(5, "签到奖励金币时，服务器发生数据错误.");
//        }
//
//		//领奖成功，查询玩家当前金币,钻石
//        $object = DB::table('t_player_info')->where('id',$this->uid)->where('status',1)->first([
//            'gold','migu_id','diamonds'
//        ]);


//		$ret_data                 = array();
//		$ret_data["add_diamonds"] = $add_diamonds;
//		$ret_data["add_gold"]     = $add_gold;
//		$ret_data["gold"]         = $object->gold;
//		$ret_data["diamonds"]     = $object->diamonds;

        DB::table('t_mail_info')->insert([
            'uid' => $uid,
            'type' => 2,
            'head' => '累计签到奖励',
            'content' => '恭喜您获得累计签到奖励。祝您游戏愉快。附带附件内容。' ,
            'rule' =>  $prize[$box_id],
            'status' => 1
        ]);
//        //把附件 加入到背包道具
//        $rule_arr = explode('|',$prize[$box_id]);
//        foreach ($rule_arr as $value) {
//            $value_arr = explode(':',$value);
//            DB::select("INSERT INTO `t_user_bag_info` (`item_id`,`uid`,`num`) VALUES ($value_arr[0],$uid,$value_arr[1]) ON DUPLICATE KEY UPDATE `num` = `num` + $value_arr[1]");
//        }

		return $this->returnData();

	}
}
